# SConscript for meetingservice project
import os
import fnmatch
import jcfutils
import shoggoth

Import('env')

envLocal = env.Clone()

# Include Path
envLocal.Append(CPPPATH = ['../include','../include/csfunified'])
envLocal.Append(CPPPATH = ['../api','../api/csfunified'])
envLocal.Append(CPPPATH = ['..', '../src'])

# Dependencies
envLocal.UseLibrary('boost')
envLocal.UseLibrary('csf2g-foundation')
envLocal.UseLibrary('csflogger')
envLocal.UseLibrary('meetings-sdk')
envLocal.UseLibrary('meetings-sdk', actions=['GET_DLLS'])
envLocal.UseLibrary('libxml2')
envLocal.UseLibrary('jcfcoreutils')
envLocal.UseLibrary('systemservice-api')
envLocal.UseLibrary('servicesframework')
envLocal.UseLibrary('jabberwerxcpp')
envLocal.UseLibrary('csfnetutils')
envLocal.UseLibrary('jansson')
envLocal.UseLibrary('ciscossl')
envLocal.UseLibrary('configservice', actions = ['COMPILE'])
envLocal.UseLibrary('contactservice', actions = ['COMPILE'])
envLocal.UseLibrary('impresenceservices', actions = ['COMPILE'])
envLocal.UseLibrary('minizip')
envLocal.UseLibrary('telemetryservice')

if envLocal.TargetPlatform() == 'windows':
    envLocal.UseLibrary('systemmonitor')
else :
    envLocal.UseLibrary('systemmonitor', actions = ['COMPILE'])

# Build/Link Settings
if envLocal.TargetPlatform() == 'windows':
    envLocal.AppendUnique(CPPFLAGS = ['/W3'])
    envLocal.AppendUnique(CPPFLAGS = ['/WX'])
    envLocal.Append(CPPDEFINES =['WIN32','CSFUNIFIED_EXPORTS', 'UNIFIED_DLL', 'USE_CONNECT_IMP', 'CSF_PERSON_STATIC', 'USE_SYSTEM_MONITOR', 'CSF_PERSON_EXPORTS', '_CRT_SECURE_NO_WARNINGS', '_SCL_SECURE_NO_WARNINGS', '_WINSOCK_DEPRECATED_NO_WARNINGS'])
    envLocal.Append(LIBS = ['Advapi32.lib'])

if envLocal.TargetPlatform() == 'macosx':
    envLocal.Append(CPPDEFINES = ['OSX', 'AT_MAC'])
    envLocal.Append(LINKFLAGS = '-install_name @executable_path/services/NewMeetingService/libNewMeetingService.dylib')
    
if envLocal.TargetPlatform() in ['ios','iossimulator']:
    envLocal.Append(CPPDEFINES = 'IOS')

# Setup Model For Code Generation
model = '../api/csfunified/services/MeetingServiceModel.xml'

# Register Builders For Generated Headers
envLocal.ShoggothCPPInterface(model)
envLocal.ShoggothCPPNotifierImpl(model)

# Register Builders For Generated Base Impl
baseImplSrcs = envLocal.ShoggothCPPBoilerplate(model)
baseImplSources = jcfutils.filterSources(baseImplSrcs, '.cpp')

# Setup Source Files
sources = jcfutils.getAllSources()
jcfutils.ignoreCommonWarnings(envLocal)

# Merge Generated Source (to trigger code generation)
sources = jcfutils.mergeSources(sources,baseImplSources)

# Setup Reflector Sources
if envLocal['JcfReflection'] :
    reflectorheadersources = envLocal.ShoggothReflectorHeader(model)
    reflectorbodysources = envLocal.ShoggothReflectorBody(model)
    sources = jcfutils.mergeSources(sources, reflectorbodysources)

# add meeting SDK wrapper source
sdkSources = []
if envLocal.TargetPlatform() == 'windows':
    sdkSources = jcfutils.getAllSources('../sdkwrapper/win32')
else :
    sdkSources = jcfutils.getAllSources('../sdkwrapper/mac')

sources = jcfutils.mergeSources(sources, sdkSources)

# Add localisation resources
if envLocal.TargetPlatform() == 'windows':
    sources.append(envLocal.RES('MeetingService.rc'))
    eventDescriptions = []

if envLocal.TargetPlatform() == 'windows':
    envLocal.InstallAs('NewMeetingService.xml', 'NewMeetingService_win.xml')
elif envLocal.TargetPlatform() == 'macosx':
    envLocal.InstallAs('NewMeetingService.xml', 'NewMeetingService_mac.xml')

# Output
if envLocal.TargetPlatform() in ['android', 'ios', 'iossimulator'] :
    resources = []
    artifacts = envLocal.StaticServiceLibrary('NewMeetingService', sources, resources)
    serviceFiles = []
else :
    resources = ['NewMeetingService.xml']
    artifacts = envLocal.SharedServiceLibrary('NewMeetingService', sources, resources)
    serviceFiles = [['NewMeetingService', [artifacts[0]], File('NewMeetingService.xml')]]

if envLocal.TargetPlatform() == 'windows':
    envLocal.Requires(artifacts, envLocal['DLLS'])

linkLibraries = jcfutils.getLinkLibraries(artifacts)

if envLocal['DEBUG']:   
    Alias('newmeetingservice-debug', [artifacts])
else:    
    Alias('newmeetingservice', [artifacts])

# Register Library
env.RegisterLibrary('newmeetingservice',
    COMPILE={
        'CPPPATH': [Dir('../include').abspath, Dir('../api').abspath]
    },
    LINK={
        'LIBS': linkLibraries
    },
    GET_SERVICE_FILES = {
        'SERVICE_FILES' : serviceFiles
    })

# Help    
Help("""
Target: newmeetingservice-debug
    Build the newmeetingservice debug library
    """)
Help("""
Target: newmeetingservice
    Build the newmeetingservice library
    """)    

# Register Aliases to build any generated wrapper libraries
dependencies = ['servicesframework']
shoggoth.registerAliases(env,'NewMeetingService', dependencies, model)

# Register Aliases for mock objects
mockSources = envLocal.ShoggothCPPMockito(model)
if envLocal['DEBUG']:
    Alias('newmeetingservice-mock-debug', [mockSources])
else:
    Alias('newmeetingservice-mock', [mockSources])